<!--  -->
<template>
  <div class="app-container">
    <div class="container">
      <el-row>
        <el-form :inline="true" :model="listQuery">
          <el-form-item label="名称">
            <el-input v-model="listQuery.name" placeholder="名称" clearable  />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSearch">查询</el-button>
            <el-button type="primary" @click="onCreate">创建</el-button>
          </el-form-item>
        </el-form>
      </el-row>
      <el-row>
        <el-table :data="list"  v-loading="listLoading" element-loading-text="拼命加载中" border fit highlight-current-row>
          <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column>
          <el-table-column prop="title" label="标题" width="145" align="center"></el-table-column>
          <el-table-column prop="name" label="名称" width="145" align="center"></el-table-column>
          <el-table-column prop="value" label="值" width="200" align="center"></el-table-column>
          <el-table-column prop="status" label="状态" width="145" align="center">
              <template #default="scope">
                <el-tag>{{ statusDesc[scope.row.status] }}</el-tag>
              </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="245">
            <template #default="scope">
              <el-button link type="primary" icon="el-icon-edit"  @click="handleEdit(scope.row)" >编辑</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <div class="pagination">
          <el-pagination
            layout="sizes, total, prev, pager, next"
            :page-sizes="[10, 20, 30]"
            v-model:page-size="listQuery.rows"
            v-model:currentPage="listQuery.page"
            @current-change="(val) => { fetchData() }"
            @size-change="(val) => { fetchData() }"
            :total="totalRecord"
          ></el-pagination>
        </div>
      </el-row>
    </div>
    <!-- 新增弹出框 -->
    <el-dialog :title="isAdd ? '新增' : '编辑'" v-model="addFormVisible" :close-on-click-modal="false"  width="600px">
      <el-form ref="form" :model="form" label-width="60px">
        <el-input v-model="form.id" type="hidden"></el-input>
        <el-form-item label="标题" :label-width="formLabelWidth">
          <el-input v-model="form.title" style="width: 300px;"></el-input>
        </el-form-item>
        <el-form-item label="名称" :label-width="formLabelWidth" prop="name" :rules="[{ required: isAdd, message: '名称不能为空', trigger: 'blur' }]">
          <el-input v-model="form.name" style="width: 300px;" :readonly="!isAdd"></el-input>
        </el-form-item>
        <el-form-item label="值" :label-width="formLabelWidth">
          <el-input v-model="form.value" style="width: 300px;"></el-input>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-select v-model="form.status" placeholder="状态" style="width: 300px;">
            <el-option label="正常" :value="0"></el-option>
            <el-option label="禁用" :value="99"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="addFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="onSave" :loading="formLoading">保 存</el-button>
          </span>
        </template>
    </el-dialog>
  </div>
</template>

<script>
import { query, save } from '@/api/config'
import { utilsMixin } from '@/mixin/utils'
export default {
  name: 'ConfigList',
  mixins: [utilsMixin],
  data() {
    return {
      list: null, // 列表数据
      listLoading: true, // 加载状态
      totalRecord: 0, // 数据总条数
      listQuery: { page: 1, rows: 10, name: '' },
      listQueryParam: { page: 1 },
      formLabelWidth: '160px',
      formLoading: false,
      addFormVisible: false, // 新增单窗
      isAdd: false, // 当前是否是新增
      form: {
        id: '',
        name: '',
        value: '',
        title: '',
        status: 0
      },
      statusDesc:{
        0:'正常',
        99:'禁用'
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      query(this.listQuery).then(response => {
        if (response.success && response.data != null) {
          this.list = response.data.records
          this.totalRecord = response.data.total
        }
        this.listLoading = false
      }).catch(() => {
        this.listLoading = false
      })
    },
    // 查询
    onSearch() {
      this.listQuery = Object.assign(this.listQuery, this.listQueryParam) // 合并对象
      this.fetchData()
    },
    clearForm() {
      this.form = {
        id: '',
        value: '',
        name: '',
        title: '',
        status: 0
      }
    },
    onCreate() {
      this.clearForm()
      this.addFormVisible = true
      this.isAdd = true
    },
    // 编辑
    handleEdit(row) {
      this.form = Object.assign(this.form, row) // 合并对象
      this.addFormVisible = true
      this.isAdd = false
    },
    onSave() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.formLoading = true
          save(this.form).then(response => {
            this.formLoading = false
            if (response.success) {
              this.$message({
                message: '保存成功！',
                type: 'success'
              })
              this.addFormVisible = false
              this.fetchData()
            } else {
              this.$message({
                message: '保存失败：' + response.msg,
                type: 'error'
              })
            }
          }).catch(() => {
            this.formLoading = false
          }) // end of createOrUpdate
        }
      }) // end of validate
    },
  }
}
</script>
